package com.firemoss.common.util.validator
{
	import flash.display.DisplayObject;
	import mx.collections.ArrayCollection;
	import mx.core.Container;
	import mx.core.UIComponent;
	import mx.events.ValidationResultEvent;
	import mx.validators.Validator;
	
	public class ValidatorHelper
	{
		public static function collectErrors(validatorArray:Array):Array
		{
			
			var validator:Validator;
			var result:ValidationResultEvent;
			var errors:Array = new Array();
			
			for each (validator in validatorArray)
			{
				result = validator.validate();
				
				if (result.type == ValidationResultEvent.INVALID)
				{
					errors.push(result.message);
				}
			}
			
			return errors;
		}
		
		public static function showErrorPopup(errors:Array):void
		{
			var popup:ValidationErrorsPopup = new ValidationErrorsPopup();
			
			popup.errors = new ArrayCollection(errors);
			
			popup.show();
		}
		
		public static function clearValidationErrors( target : Container ) : void
		{
			for each ( var item : DisplayObject in target.getChildren() )
			{
				if ( item is UIComponent ) {
					UIComponent( item ).errorString = "";
				}
				if ( item is Container && Container( item ).numChildren > 0 )
				{
					clearValidationErrors( item as Container );				
				}
			}	
		}
		
	}
}